package com.instalinks.android.lib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;

import android.content.Context;

public class Config {

	private String mFileName = "";
	protected final Context mContext;
	protected final boolean mUseSDCard;
	private Hashtable<String, String> mHtConfig;

	public Config(Context context, String configName, boolean useSDCard) {

		this.mContext = context;
		this.mFileName = configName;
		this.mUseSDCard = useSDCard;

		try {
			readConfig();
		} catch (Exception e) {
			mHtConfig = new Hashtable<String, String>();
		}
	}
	
	public void rePopulate(){
		try {
			readConfig();
		} catch (Exception e) {
			mHtConfig = new Hashtable<String, String>();
		}
	}

	protected Context getContext() {
		return this.mContext;
	}

	public boolean isConfigExists() {
		try {
			readConfig();
			return true;
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		} catch (ClassNotFoundException cnf) {
			return true;
		}
	}

	protected boolean hasKey(String key){
		return mHtConfig.containsKey(key);
	}
	
	protected String getValue(String key) {

		if (mHtConfig.containsKey(key))
			return mHtConfig.get(key);
		else
			return "";
	}

	protected int getIntValue(String key) {
		if (mHtConfig.containsKey(key))
			return Integer.valueOf(mHtConfig.get(key));
		else
			return 0;
	}

	protected boolean getBooleanValue(String key) {
		if (mHtConfig.containsKey(key))
			return Boolean.valueOf(mHtConfig.get(key));
		else
			return false;
	}

	protected void setIntValue(String key, int value)
			throws FileNotFoundException, IOException {
		setValue(key, String.valueOf(value));
	}

	protected void setBooleanValue(String key, boolean value)
			throws FileNotFoundException, IOException {
		setValue(key, String.valueOf(value));
	}

	protected void setValue(String key, String value)
			throws FileNotFoundException, IOException {
		mHtConfig.put(key.toString(), value);
		saveConfig();
	}

	@SuppressWarnings("unchecked")
	protected void readConfig() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		synchronized (this) {
			FileInputStream fis = mContext.openFileInput(mFileName);
			ObjectInputStream ois = new ObjectInputStream(fis);
			mHtConfig = (Hashtable<String, String>) ois.readObject();

			ois.close();
			fis.close();
		}
	}

	private void saveConfig() throws FileNotFoundException, IOException {
		synchronized (this) {
			FileOutputStream fos = mContext.openFileOutput(mFileName,
					Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			oos.writeObject(mHtConfig);

			oos.close();
			fos.close();
		}
	}

	protected void clearConfig() throws FileNotFoundException, IOException {
		synchronized (this) {
			mHtConfig.clear();
			saveConfig();
		}
	}

	protected boolean deleteConfig() {
		synchronized (this) {
			return mContext.deleteFile(mFileName);
		}
	}
}
